import { _decorator, Collider2D, Component, Contact2DType, Node, RigidBody2D, Sprite, Vec2 } from 'cc';
import { COLLISION_NAME, GlobalData } from '../../Data/GlobalData';
import { GameScene } from './GameScene';
import { Board } from './Board';
import { ElementLoad } from './ElementLoad';
import { CoinLoad } from './CoinLoad';
const { ccclass, property } = _decorator;

@ccclass('Ball')
export class Ball extends Component {

    //左右移动速度
    private MoveSpeed: number = 10;

    //起跳速度
    private JumpSpeed: number = 50;

    //是否碰到篮板了
    private IsBoard: boolean = false;

    //是否碰到篮网
    private IsNet: boolean = false;

    //是否是最高点
    private IsTop: boolean = false;

    //最高点位置
    private TopPoint: number = 700;

    //元素层
    @property(Node)
    private ElementLoad: Node = null;

    //游戏场景
    @property(Node)
    private GameScene: Node = null;

    //金币层
    @property(Node)
    private CoinLoad: Node = null;

    protected start(): void {
        this.node.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT, this.OnTriggerEnter, this);
    }

    /**
     * 更新球的样式
     */
    public UpdateBallSytle(_index: number) {
        this.node.getComponent(Sprite).spriteFrame = app.resManager.SpriteFrames["game_normalball" + (_index + 1)];
    }

    /**
     * 起跳
     */
    public IamJump(_isleft: boolean) {
        if (this.ElementLoad.getComponent(ElementLoad).ControlTime <= 0) return;

        this.IsBoard = false;
        this.IsNet = false;
        this.IsTop = false;

        if (this.node.getPosition().y >= this.TopPoint) this.IsTop = true;

        this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(_isleft ? -this.MoveSpeed : this.MoveSpeed, this.JumpSpeed)
    }

    /**
     * 
     */
    private OnTriggerEnter(_selfCollider: Collider2D, _otherCollider: Collider2D) {
        switch (_otherCollider.group) {
            case COLLISION_NAME.Net:
                this.IsNet = true;
                break;
            case COLLISION_NAME.Board:
                this.IsBoard = true;
                break;
            case COLLISION_NAME.Score:
                if (this.node.getComponent(RigidBody2D).linearVelocity.y < 0 && !GlobalData.GameOver) {
                    this.GameScene.getComponent(GameScene).IsLeft = !this.GameScene.getComponent(GameScene).IsLeft;
                    this.ElementLoad.getComponent(ElementLoad).UpdateControlTime();
                    _otherCollider.node.parent.getComponent(Board).PlayAni(() => {
                        this.ElementLoad.getComponent(ElementLoad).UpdateScoreTip(this.IsBoard, this.IsNet, this.IsTop);
                        this.GameScene.getComponent(GameScene).GameScore();
                    }, this.IsNet)
                }
                break;
            case COLLISION_NAME.Floor:
                if (this.ElementLoad.getComponent(ElementLoad).ControlTime <= 0) {
                    this.GameScene.getComponent(GameScene).GameOver();
                }
                break;
            case COLLISION_NAME.Prop:
                this.scheduleOnce(() => {
                    console.warn("碰撞金币", 1111, _otherCollider.node.getPosition())
                    this.CoinLoad.getComponent(CoinLoad).HitCoin(_otherCollider.node);
                    GlobalData.GameCoin++;
                    this.ElementLoad.getComponent(ElementLoad).UpdateCoin();
                });
                break;
        }
    }

}


